package com.nut.spp.service.impl;

import com.nut.spp.common.Result;
import com.nut.spp.entity.SysFile;
import com.nut.spp.enums.FilePathEnum;
import com.nut.spp.mapper.SysFileMapper;
import com.nut.spp.service.SysFileService;
import com.nut.spp.util.OSSUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@Service
public class SysFileServiceImpl implements SysFileService {

    @Autowired
    private OSSUtil ossUtil;

    @Autowired
    private SysFileMapper sysFileMapper;

    @Override
    public Result<String> upload(MultipartFile multipartFile) {
        return upload(multipartFile, FilePathEnum.Hospital);
    }

    @Override
    public Result<String> upload(MultipartFile multipartFile, FilePathEnum pathEnum) {
        try {
            SysFile sysFile = ossUtil.uploadFile(multipartFile, pathEnum);
            sysFile.setSize(multipartFile.getSize());
            sysFileMapper.insert(sysFile);
            return Result.ok(sysFile.getUrl());
        } catch (Exception e) {
            log.error("upload_error", e);
            return Result.fail(e.getMessage());
        }
    }

    public static void main(String[] args) throws Exception {

//        System.out.println(LocalDate.now().getYear());
//        System.out.println(LocalDate.now().getMonthValue());

    }
}
